// subPages/mine/newAddress/newAddress.js
const app = getApp()
var util =  require('../../../utils/util.js');
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/login/triangleDown.png',
    upImg: '/resources/login/triangleUp.png',
    region: ['请选择', '请选择', '请选择'],
    customItem: '请选择',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.isOne){
      that.setData({
        isOne: options.isOne,
        isCheck: options.isOne,
        isEditor: false,
      })
    }
    if(options.list != 'undefined' && options.list != null)
    {
      var list = JSON.parse(options.list)
      that.setData({
        isEditor: true,
        name: list.name,
        phone: list.phone,
        isRegion: true,
        region: [list.sheng,list.shi,list.qu],
        address: list.address,
        isCheck: list.status,
        id: list.id,
        isOne: '0'
      })
    }
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
        }
      }
    }
    that.setData({
      regionImgSrc: that.data.downImg,
    })
  },

  //地区选择
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      regionImgSrc: this.data.downImg,
      isRegion: true
    })
  },

  bindRegionCancel:function(){
    this.setData({
      regionImgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(e.currentTarget.dataset.type == "地区"){
      if(that.data.regionImgSrc == that.data.downImg)
      {
        that.setData({
          regionImgSrc: that.data.upImg
        })
      }
    }
  },

  //默认地址选择
  addressCheck:function(){
    var that = this
    if(that.data.isOne == '1'){
      util.showModal('提示','第一次添加为默认收货地址')
      return
    }
    if(that.data.isCheck == '1'){
      that.setData({
        isCheck: 0
      })
    }else{
      that.setData({
        isCheck: 1
      })
    }
  },

  formSubmit:function(e){
    var that = this
    if(flag){
      return
    }
    flag = true
    var name = e.detail.value.name;
    var phone = e.detail.value.phone;
    var region = e.detail.value.region
    var address =  e.detail.value.address;
    if (name == "" || address == "" || phone == ""){
      util.showModal('提示','请填写完整信息')
      flag = false
      return
    }
    if(name.length > 0)
    {
      var type= 'name'
      var validation = util.validation(name,type)
      if(!validation){
        util.showModal('提示','姓名输入不正确')
        flag = false
        return
      }
    }else{
      util.showModal('提示','请输入姓名')
      flag = false
      return
    }
    for(var i in region){
      if(region[i] == '请选择'){
        util.showModal('提示','请选择地区')
        flag = false
        return 
      }
    }
    if(phone.length > 0 && phone != '')
    {
      var type= 'phone'
      var validation = util.validation(phone,type)
      if(!validation){
        util.showModal('提示','手机号输入不正确')
        flag = false
        return
      }
    }else{
      util.showModal('提示','请输入手机号')
      flag = false
      return
    }
    that.setData({
      name: name,
      region: region,
      address: address,
      phone: phone,
    })
    that.save(name,phone,region,address)
  },

  //保存地址
  save:function(name,phone,region,address){
    var that = this
    if(that.data.isEditor){
      //修改
      wx.request({
        url: app.globalData.apiUrl + '/save_user_address',
        data: {
          id: that.data.id,
          user_id: that.data.userId,
          name: name,
          phone: that.data.phone,
          sheng: region[0],
          shi: region[1],
          qu: region[2],
          address: address,
          status: that.data.isCheck
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
        },
        success: function (res) {
          console.log(res)
          if(res.data == '1'){
            util.showToast('修改成功','success',2000)
            setTimeout(function(){
              wx.navigateBack({
                delta: 1, // 回退前 delta(默认为1) 页面
              })
              flag = false
            },2000)
          }else{
            flag = false
            util.showModal('提示','修改失败，请重新保存')
          }
        },
        fail: function (res) {
          flag = false
          util.showModal('提示','修改失败，请重新保存')
        }
      })
    }else{
      //新增
      wx.request({
        url: app.globalData.apiUrl + '/add_address',
        data: {
          user_id: that.data.userId,
          name: name,
          phone: that.data.phone,
          sheng: region[0],
          shi: region[1],
          qu: region[2],
          address: address,
          status: that.data.isCheck
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
        },
        success: function (res) {
          console.log(res)
          if(res.data == '1'){
            util.showToast('保存成功','success',2000)
            setTimeout(function(){
              wx.navigateBack({
                delta: 1, // 回退前 delta(默认为1) 页面
              })
              flag = false
            },500)
          }else{
            flag = false
            util.showModal('提示','保存失败，请重新保存')
          }
        },
        fail: function (res) {
          flag = false
          util.showModal('提示','保存失败，请重新保存')
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})